package cn.net.dpi.core.trace;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;

public interface TraceOuter {

    default String prettyOut() {
        return prettyOut(null);
    }

    String prettyOut(Logger logger);

    default String jsonOut(boolean pretty) {
        return jsonOut(pretty, null);
    }

    default String jsonOut(boolean pretty, Logger logger) {

        String json = "";
        ObjectMapper objectMapper = new ObjectMapper();

        try {
            json = pretty
                    ? objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(this)
                    : objectMapper.writeValueAsString(this);

        } catch (JsonProcessingException e) {
            json = "parse error: " + e.getMessage();
        }

        return json;
    }
}
